home *** CD-ROM | disk | FTP | other *** search
- /*-----------------------------------------------------------------------*/
- /* filename - tdemo.cpp */
- /* */
- /* function(s) */
- /* test module for the THandler class */
- /* */
- /* */
- /* author - Michael Newton */
- /*-----------------------------------------------------------------------*/
-
- /*-----------------------------------------------------------------------*/
- /* */
- /* TDEMO.CPP */
- /* */
- /* THandler - Version 1.01 */
- /* Copyright (C) 1992, 1993 Michael Newton */
- /* All Rights Reserved. */
- /* */
- /*-----------------------------------------------------------------------*/
-
-
- // Define Turbo Vision classes used ***************************************
-
- #define Uses_TApplication
- #define Uses_TButton
- #define Uses_TCheckBoxes
- #define Uses_TDeskTop
- #define Uses_TDialog
- #define Uses_TDisplay
- #define Uses_TEvent
- #define Uses_TGroup
- #define Uses_TKeys
- #define Uses_TMenu
- #define Uses_TMenuBar
- #define Uses_TMenuItem
- #define Uses_TObject
- #define Uses_TPalette
- #define Uses_TProgram
- #define Uses_TRect
- #define Uses_TScreen
- #define Uses_TStatusDef
- #define Uses_TStatusItem
- #define Uses_TStatusLine
- #define Uses_TStaticText
- #define Uses_TSItem
- #define Uses_TSubMenu
- #define Uses_TView
- #define Uses_TWindow
- #define Uses_MsgBox
- #define Uses_fpstream
- #define Uses_ipstream
- #define Uses_opstream
-
- #include <tv.h>
-
-
-
-
-
-
- // CRTL header files ******************************************************
-
- #if !defined __STDLIB_H
- #include <stdlib.h>
- #endif
-
- #if !defined __STDIO_H
- #include <stdio.h>
- #endif
-
- #if !defined __STRING_H
- #include <string.h>
- #endif
-
- #if !defined __STRSTREAM_H
- #include <strstrea.h>
- #endif
-
- #if !defined __FSTREAM_H
- #include <fstream.h>
- #endif
-
- #if !defined __IOMANIP_H
- #include <iomanip.h>
- #endif
-
- #if !defined __DOS_H
- #include <dos.h>
- #endif
-
- #if !defined __DIR_H
- #include <dir.h>
- #endif
-
- #if !defined __ALLOC_H
- #include <alloc.h>
- #endif
-
-
-
-
-
-
-
- // Non-CRTL header files **************************************************
-
- #if !defined __THANDLER_HPP
- #include "thandler.hpp"
- #endif
-
-
-
-
-
-
-
- // Command constants ******************************************************
-
- const cmTests = 1000,
- cmDriveA = 1001,
- cmDriveB = 1002,
- cmLpt1 = 1003,
- cmAux = 1004,
- cmToggle = 1005,
- cmAbout = 1006,
- cmMode = 1007,
- hcNone = hcNoContext;
-
-
-
-
-
-
-
- // Non-member function declarations ***************************************
-
- void exitfunc(void);
-
-
-
-
-
-
- // TMenuItem **************************************************************
-
- TMenuItem& operator +(TMenuItem& item1, TMenuItem& item2)
- {
- TMenuItem *p = &item1;
- while(p->next != NULL)
- p = p->next;
- p->next = &item2;
- return item1;
- }
-
-
-
-
-
-
- // TDemoDialog ************************************************************
-
- class TDemoDialog : public TDialog
- {
- public:
- TDemoDialog(const TRect& bounds, const char *aTitle) :
- TDialog(bounds, aTitle),
- TWindowInit(TDemoDialog::initFrame){};
- virtual void handleEvent(TEvent& event);
- };
-
-
- void TDemoDialog::handleEvent(TEvent& event)
- {
- TDialog::handleEvent(event);
-
- switch(event.what)
- {
- case evCommand:
- switch(event.message.command)
- {
- case cmDriveA: // Our commands
- case cmDriveB:
- case cmLpt1:
- case cmAux:
- if((state & sfModal) != 0)
- {
- endModal(event.message.command);
- clearEvent(event);
- }
- break;
- }
- break;
- }
- }
-
-
-
-
-
-
-
-
- // THandlerDemo ***********************************************************
-
- // Color palette
- #define cpC \
- "\x09\x17\x18\x1F\x0E\x04\x0F\x10\x17\x0E" \
- "\x09\x1A\x17\x1C\x1E\x37\x3F\x0E\x0C\x13" \
- "\x30\x3C\x00\x78\x70\x0E\x0E\x13\x70\x40" \
- "\x1E\x78\x70\x0E\x6E\x4E\x70\x70\x7E\x7E" \
- "\x30\x3E\x3F\x37\x3B\x78\x70\x7E\x7E\x03" \
- "\x4F\x7E\x5E\x55\x0B\x3E\x03\x4F\x0E\x07" \
- "\x0F\x00\x00"
-
- // Black and white palette
- #define cpB \
- "\x70\x70\x78\x7F\x07\x07\x0F\x07\x0F\x07" \
- "\x70\x70\x07\x70\x00\x07\x0F\x07\x70\x70" \
- "\x07\x70\x00\x70\x7F\x7F\x70\x07\x70\x07" \
- "\x00\x70\x7F\x7F\x70\x07\x70\x70\x7F\x7F" \
- "\x07\x0F\x0F\x78\x0F\x78\x07\x0F\x0F\x0F" \
- "\x70\x0F\x07\x70\x70\x70\x07\x70\x0F\x07" \
- "\x07\x00\x00"
-
- // Monochrome palette
- #define cpM \
- "\x70\x07\x07\x0F\x70\x70\x70\x07\x0F\x07" \
- "\x70\x70\x07\x70\x00\x07\x0F\x07\x70\x70" \
- "\x07\x70\x00\x70\x70\x70\x07\x07\x70\x07" \
- "\x00\x70\x70\x70\x07\x07\x70\x70\x70\x0F" \
- "\x07\x07\x0F\x70\x0F\x70\x07\x0F\x0F\x07" \
- "\x70\x07\x07\x70\x07\x07\x07\x70\x0F\x07" \
- "\x07\x00\x00"
-
-
- // Option flags for toggles dialog
- const ofSystem = 1;
- const ofSounds = 2;
-
-
- class THandlerDemo : public TApplication
- {
- public:
- THandlerDemo();
- virtual void shutDown();
- virtual TPalette& getPalette() const;
- private:
- void handleEvent(TEvent& Event);
- static TDeskTop *initDeskTop(TRect r);
- static TMenuBar *initMenuBar(TRect r);
- static TStatusLine *initStatusLine(TRect r);
- ushort toggleData; // Toggles dialog data
- Boolean systemCalls; // True = use system calls
- THandler *newHandler; // Pointer to error handler object
- ushort testDevices(); // Device test dialog
- void toggleDialog(); // Toggles dialog
- void aboutDialog(); // About dialog
- void toggleVideo(); // Toggle video mode
- };
-
-
- THandlerDemo::THandlerDemo() :
- TProgInit(initStatusLine, initMenuBar, initDeskTop)
- {
- // Create the THandler object with sounds on
- newHandler = new THandler(True);
-
- // Initialize toggles dialog data
- toggleData = ofSounds;
- systemCalls = False;
-
- // Draw the desktop
- setState(sfExposed, True);
- redraw();
-
- // Show "About" box
- TEvent event;
- event.what = evCommand;
- event.message.command = cmAbout;
- putEvent(event);
- }
-
-
- void THandlerDemo::shutDown()
- {
- if(newHandler != 0)
- delete newHandler;
-
- newHandler = 0;
-
- TProgram::shutDown();
- }
-
-
- TDeskTop *THandlerDemo::initDeskTop(TRect r)
- {
- r.a.y++;
- r.b.y--;
- TProgram::application->setState(sfExposed, False);
- return new TDeskTop(r);
- }
-
-
- TPalette& THandlerDemo::getPalette() const
- {
- static TPalette c(cpC, sizeof(cpC) - 1);
- static TPalette b(cpB, sizeof(cpB) - 1);
- static TPalette m(cpM, sizeof(cpM) - 1);
- static TPalette *palettes[] = {&c, &b, &m};
- return *(palettes[appPalette]);
- }
-
-
- void THandlerDemo::handleEvent(TEvent& event)
- {
- TApplication::handleEvent(event);
-
- if(event.what == evCommand)
- {
- switch(event.message.command)
- {
- // Call testDevices until user selects Cancel
- case cmTests:
- while(testDevices() != cmCancel);
- break;
- case cmToggle:
- toggleDialog();
- break;
- case cmAbout:
- aboutDialog();
- break;
- case cmMode:
- toggleVideo();
- break;
- default:
- return;
- }
- clearEvent(event);
- }
- }
-
-
- TMenuBar *THandlerDemo::initMenuBar(TRect r)
- {
- r.b.y = r.a.y + 1;
-
- TMenuItem *sys;
- sys = new TMenuItem("~\360~", kbAltSpace, new TMenu(
- *new TMenuItem("~A~bout", cmAbout, kbAltA, hcNone) +
- *new TMenuItem("~V~ideo mode", cmMode, kbAltV, hcNone)));
-
- TMenuItem *exit;
- exit = new TMenuItem("E~x~it", cmQuit, kbAltX, hcNone, 0);
-
- TMenuItem *tests;
- tests = new TMenuItem("~T~ests", cmTests, kbAltT, hcNone, 0);
-
- TMenuItem *togs;
- togs = new TMenuItem("T~o~ggles", cmToggle, kbAltO, hcNone, 0);
-
- return(new TMenuBar(r, new TMenu(*sys + *exit + *tests + *togs)));
- }
-
-
- TStatusLine *THandlerDemo::initStatusLine(TRect r)
- {
- r.a.y = r.b.y - 1;
-
- return new TStatusLine(r,
- *new TStatusDef(0, 0xFFFF) +
- *new TStatusItem("~Alt-X~ Exit", kbAltX, cmQuit) +
- *new TStatusItem("~F10~ Menu", kbF10, cmMenu));
- }
-
-
- void THandlerDemo::toggleDialog()
- {
- TDialog *dialog = new TDialog(TRect(18, 0, 48, 8), "Toggles");
- TView *c = new TCheckBoxes(TRect(2, 2, 26, 4),
- new TSItem("Use ~S~ystem calls",
- new TSItem("Use s~O~und", 0)));
- c->options |= ofCenterX;
- dialog->insert(c);
-
- dialog->insert(new TButton(TRect(2, 5, 14, 7),
- "~O~k", cmOK, bfDefault));
- dialog->insert(new TButton(TRect(16, 5, 28, 7),
- "~C~ancel", cmCancel, bfNormal));
-
- dialog->selectNext(True);
-
- // Execute the dialog box
- if(validView(dialog))
- {
- dialog->setData(&toggleData);
- ushort result = deskTop->execView(dialog);
- if(result != cmCancel)
- {
- dialog->getData(&toggleData);
- newHandler->setSound((toggleData & ofSounds) ? True : False);
- systemCalls = ((toggleData & ofSystem) ? True : False);
- }
- destroy(dialog);
- }
- }
-
-
- void THandlerDemo::toggleVideo()
- {
- // Toggle video mode
- ushort mode = TScreen::screenMode ^ TDisplay::smFont8x8;
- setScreenMode(mode);
- }
-
-
- void THandlerDemo::aboutDialog()
- {
- char title[] = "About";
- char *line[] =
- {
- "THandler - Version 1.01",
- "(C) Copyright 1992, 1993 Michael Newton",
- "Demonstrates the THandler hardware error handler"
- };
-
- TDialog *dialog = new TDialog(TRect(0, 0, 60, 10), title);
- dialog->options |= ofCentered;
-
- TStaticText *t;
- int len = strlen(line[0]);
- t = new TStaticText(TRect(0, 2, len, 3), line[0]);
- t->options |= ofCenterX;
- dialog->insert(t);
-
- len = strlen(line[1]);
- t = new TStaticText(TRect(0, 3, len, 4), line[1]);
- t->options |= ofCenterX;
- dialog->insert(t);
-
- len = strlen(line[2]);
- t = new TStaticText(TRect(0, 5, len, 6), line[2]);
- t->options |= ofCenterX;
- dialog->insert(t);
-
- TButton *b;
- b = new TButton(TRect(10, 7, 50, 9), "~O~k", cmCancel, bfDefault);
- dialog->insert(b);
-
- if(validView(dialog))
- {
- deskTop->execView(dialog);
- destroy(dialog);
- }
- }
-
-
- ushort THandlerDemo::testDevices()
- {
- char msg1[] = "Select a device to access:";
- char msg2[] = "Make sure there is no disk in the drive";
- char msg3[] = "and the printer is turned off.";
-
- TDemoDialog *dialog = new TDemoDialog(TRect(0, 0, 50, 14), "Devices");
- dialog->options |= ofCentered;
-
- // Messages to user
- TStaticText *t;
- int len = strlen(msg1);
- t = new TStaticText(TRect(0, 2, len, 3), msg1);
- t->options |= ofCenterX;
- dialog->insert(t);
-
- len = strlen(msg2);
- t = new TStaticText(TRect(0, 4, len, 5), msg2);
- t->options |= ofCenterX;
- dialog->insert(t);
-
- len = strlen(msg3);
- t = new TStaticText(TRect(0, 5, len, 6), msg3);
- t->options |= ofCenterX;
- dialog->insert(t);
-
- // Buttons
- TButton *b;
- b = new TButton(TRect(2, 7, 24, 9), "Drive ~A~:", cmDriveA, bfNormal);
- dialog->insert(b);
-
- b = new TButton(TRect(26, 7, 48, 9), "Drive ~B~:", cmDriveB, bfNormal);
- dialog->insert(b);
-
- b = new TButton(TRect(2, 9, 24, 11), "~L~PT1:", cmLpt1, bfNormal);
- dialog->insert(b);
-
- b = new TButton(TRect(26, 9, 48, 11), "AU~X~:", cmAux, bfNormal);
- dialog->insert(b);
-
- b = new TButton(TRect(2, 11, 48, 13), "~C~ancel", cmCancel, bfDefault);
- dialog->insert(b);
-
- char msg[] = "Test\n";
- static ushort result = cmCancel;
- fstream *f;
-
- if(validView(dialog))
- {
- result = deskTop->execView(dialog);
- if(result != cmCancel)
- {
- switch(result)
- {
- // Attempt to access floppy drive A:
- case cmDriveA:
- if(systemCalls)
- {
- newHandler->suspend();
- system("dir a:\\");
- newHandler->resume();
- }
- else
- {
- char adrive[] = "a:\\filename.ext";
- f = new fstream(adrive, ios::in);
- if(f != 0)
- delete f;
- }
- break;
- // Attempt to access floppy drive B:
- case cmDriveB:
- if(systemCalls)
- {
- newHandler->suspend();
- system("dir b:\\");
- newHandler->resume();
- }
- else
- {
- char bdrive[] = "b:\\filename.ext";
- f = new fstream(bdrive, ios::in);
- if(f != 0)
- delete f;
- }
- break;
- // Attempt to access the printer:
- // NOTE: The system command below assumes you have a
- // config.sys file on drive C:
- case cmLpt1:
- if(systemCalls)
- {
- newHandler->suspend();
- system("type c:\\config.sys > prn");
- newHandler->resume();
- }
- else
- fputs(msg, stdprn);
- break;
- // Attempt to access COM1:
- // NOTE: The system command below assumes you have a
- // config.sys file on drive C:
- case cmAux:
- if(systemCalls)
- {
- newHandler->suspend();
- system("type c:\\config.sys > aux");
- newHandler->resume();
- }
- else
- fputs(msg, stdaux);
- break;
- }
- }
- destroy(dialog);
- }
-
- return(result);
- }
-
-
-
-
- // Non-member functions ***************************************************
-
-
- void exitfunc(void)
- {
- cout << flush
- << endl
- << "Thank you for testing THandler!"
- << endl
- << ends;
- }
-
-
- #pragma exit exitfunc 31
-
-
- int main()
- {
- THandlerDemo program;
- program.run();
- program.shutDown();
- return(0);
- }
-
-
-
- // End of TDEMO.CPP
-